/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/07/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/ 
package com.application.yfmaterieldepart.struts.actions;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;
import org.apache.struts.actions.DispatchAction;

import com.application.yfmaterieldepart.commons.Constants;
import com.application.yfmaterieldepart.entity.YfMaterielDepart;
import com.application.yfmaterieldepart.service.YfMaterielDepartService;
import com.application.yfmaterieldepart.struts.form.YfMaterielDepartForm;
import com.manager.commons.CommonUtil;
import com.manager.commons.RandomKey;
import com.manager.commons.page.PageBean;
 /**
 * 
 * Description:YfMaterielDepartAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060    <br />
 * 
 * History: <br />
 * 
 * 02/07/2012 
 * 
 */
public class YfMaterielDepartAction extends DispatchAction
{
       
       /**
	 * Service层实例
	 */
	private YfMaterielDepartService yfMaterielDepartService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
        private YfMaterielDepart yfMaterielDepart;


        /**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getYfMaterielDepartPartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchYfMaterielDepart" + "开始");
		YfMaterielDepartForm yfMaterielDepart = (YfMaterielDepartForm)form;
		if(yfMaterielDepart.getType()==null){
			Long type = Long.valueOf(request.getParameter("type"));
			yfMaterielDepart.setType(type);
		}
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List yfMaterielDepartList = null;
		resultSize = yfMaterielDepartService.getAllCountYfMaterielDepart(yfMaterielDepart);
		pageBean = PageBean.getPageBean("yfMaterielDepartTab", resultSize,request);
		yfMaterielDepartList = yfMaterielDepartService.searchYfMaterielDepart(yfMaterielDepart,pageBean);
		request.setAttribute("yfMaterielDepartList", yfMaterielDepartList);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		log.debug("searchYfMaterielDepart" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

        /**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddYfMaterielDepart(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toAddYfMaterielDeparts");
		YfMaterielDepartForm yfMaterielDepartForm = (YfMaterielDepartForm) form;
		yfMaterielDepartForm.setInitiateYear(Long.valueOf(CommonUtil.getCurrentYear()));
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addYfMaterielDepart(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addYfMaterielDeparts");
		YfMaterielDepart yfMaterielDepart = this.getYfMaterielDepart(form);
                yfMaterielDepart.setId(RandomKey.generateID_Long_19());
		 yfMaterielDepart.setOperateid(CommonUtil.getCurrentUser(request).getId());
		 yfMaterielDepart.setCreateDate(new Date());
		
		this.yfMaterielDepartService.insertObject(yfMaterielDepart);
	    ActionRedirect redirect = new ActionRedirect(mapping.findForward(Constants.SUCCESS)); 
	    redirect.addParameter("type", yfMaterielDepart.getType());
		return redirect;
	}
        
	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteYfMaterielDepart(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("deleteYfMaterielDeparts");
		YfMaterielDepartForm yfMaterielDepartForm = (YfMaterielDepartForm) form;
		this.yfMaterielDepartService.deleteYfMaterielDeparts(yfMaterielDepartForm);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateYfMaterielDepart(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toUpdateYfMaterielDeparts");
		YfMaterielDepartForm yfMaterielDepartForm = (YfMaterielDepartForm) form;
		yfMaterielDepart = (YfMaterielDepart) this.yfMaterielDepartService.searchYfMaterielDepart(yfMaterielDepartForm).get(0);
		request.setAttribute(Constants.YFMATERIELDEPART, yfMaterielDepart);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

        
	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateYfMaterielDepart(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("updateYfMaterielDeparts");
		YfMaterielDepart yfMaterielDepart = this.getYfMaterielDepart(form);
	    yfMaterielDepart.setOperateid(CommonUtil.getCurrentUser(request).getId());
		 yfMaterielDepart.setCreateDate(new Date());
		this.yfMaterielDepartService.updateObject(yfMaterielDepart);
	    ActionRedirect redirect = new ActionRedirect(mapping.findForward(Constants.SUCCESS)); 
	    redirect.addParameter("type", yfMaterielDepart.getType());
		return redirect;
	}
        
	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewYfMaterielDepart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("viewYfMaterielDepart");

		YfMaterielDepartForm yfMaterielDepartForm = (YfMaterielDepartForm) form;
		yfMaterielDepart = (YfMaterielDepart) this.yfMaterielDepartService.searchYfMaterielDepart(yfMaterielDepartForm).get(0);
		request.setAttribute(Constants.YFMATERIELDEPART, yfMaterielDepart);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

	// get--set
	/**
	 * @return the YfMaterielDepartService
	 */
	public YfMaterielDepartService getYfMaterielDepartService() {
		return this.yfMaterielDepartService;
	}

	/**
	 * @param YfMaterielDepartService
	 *  the YfMaterielDepartService to set
	 */
	public void setYfMaterielDepartService(YfMaterielDepartService yfMaterielDepartService) {
		this.yfMaterielDepartService = yfMaterielDepartService;
	}

	/**
	 * 获取YfMaterielDepart实体
	 * @param ActionForm
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @return the YfMaterielDepart
	 */
	public YfMaterielDepart getYfMaterielDepart(ActionForm form) throws IllegalAccessException, InvocationTargetException {
		YfMaterielDepartForm yfMaterielDepartForm = (YfMaterielDepartForm) form;
		yfMaterielDepart = new YfMaterielDepart();
		BeanUtils.copyProperties(yfMaterielDepart, yfMaterielDepartForm);
		return yfMaterielDepart;
	}
    
        public void setYfMaterielDepart(YfMaterielDepart yfMaterielDepart) {
		this.yfMaterielDepart = yfMaterielDepart;
	}

}
